// pages/resume/hourly/detail.js
const config = getApp().globalData.config
const T = getApp().globalData.T
import { collect, isCollect} from '../../../utils/collect.js'
let token = ''
let userId = -1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collectStatus: 0,
    id: -1, // 用户id
    resumeId:-1,//简历id
    status: -1,//简历状态
    resumeDeliverId: -1,//面试id
    noOrder: true,//没有预约面试
    orderData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      status: options.status,
      resumeId: options.resumeId,// 简历id
      id: options.id,//用户id
      resumeDeliverId: options.resumeDeliverId,//面试id
    })
    console.log(options);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    token = getApp().globalData.token;
    userId = getApp().globalData.userId;
    this.getDetail();
    this.getCollectStatus();
    if (this.data.resumeDeliverId) {
      this.getOrderData();
    }
  },

  getDetail() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'resume/hourly/detail/' + this.data.id,
      method: 'GET',
      data: {
        token: token,
      },
      success: (res) => {
        this.setData({
          obj: res
        })
      }
    })
  },

  getCollectStatus() {
    isCollect(this.data.id, 4).then(res => {
      this.setData({
        collectStatus: res
      })
    })
  },
  getOrderData() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'interview',
      method: 'GET',
      data: {
        token: token,
        resumeDeliverId: this.data.resumeDeliverId
      },
      success: (res) => {
        if (res.id == 0) {
          this.setData({
            noOrder: true
          })
        } else {
          this.setData({
            noOrder: false,
            orderData: res
          })
        }
      }
    })
  },
  /**
 * 预约面试
 */
  onOrder() {
    console.log(this.data.resumeId)
    let obj = { candidateId: this.data.id, resumeId: this.data.resumeId, type: 1 }
    wx.navigateTo({
      url: '../order/order?' + T.param(obj),
    })    },

  onCollect() {
    let _param = {
      token: token,
      userId: userId,
      type: 4,
      relatedId: this.data.resumeId
    }
    collect(_param).then((res) => {
      T.toast({
        id: '#toast',
        text: this.data.collectStatus ? '取消成功' : '收藏成功'
      })
      this.setData({
        collectStatus: 1 ^ this.data.collectStatus
      })
    })
  }
})